<template>
  <!-- tab基本信息表单  -->
  <el-scrollbar style="height: 100%;margin-top:10px;">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="90px"
      class="form-input-m"
      inline-message
    >

      <el-form-item label="编号" prop="boxCode">
        <el-input v-model="formData.boxCode" placeholder="请输入编号" clearable></el-input>
      </el-form-item>
      <el-form-item label="条码号" prop="boxBarcode">
        <el-input v-model="formData.boxBarcode" placeholder="请输入条码号" clearable></el-input>
      </el-form-item>
      <el-form-item label="架层数" prop="layerNum">
        <el-input v-model="formData.layerNum" placeholder="请输入架层" clearable></el-input>
      </el-form-item>
      <el-form-item label="冷藏温度" prop="frigTemp">
        <el-input v-model="formData.frigTemp" placeholder="请输入冷藏温度" clearable></el-input>
      </el-form-item>
      <el-form-item label="冷冻温度" prop="freezeTemp">
        <el-input v-model="formData.freezeTemp" placeholder="请输入冷冻温度" clearable></el-input>
      </el-form-item>
      <el-form-item label="位置排序" prop="seq">
        <el-input v-model="formData.seq" placeholder="请输入位置排序" clearable></el-input>
      </el-form-item>
      <el-form-item label="所属科室" prop="deptName">
        <SelectDataGrid :url="'/api/sys/getDepartmentList'"
        :column="[
                        { field: 'code', title: '科室编码' }, //列标题
                        { field: 'name', title: '科室名称' }
              ]"
        :searchParam="{ orgId }" :isPage="false" @SelectCellClick="
                  (e) => {
                    selectRow(e, rowIndex);
                  }
                " :width="'200px'">
        </SelectDataGrid>
      </el-form-item>

      <el-form-item label="" prop="">
        <el-button type="primary" @click="saveForm">保存</el-button>
      </el-form-item>
    </el-form>

    <!-- 配置扩展属性弹框 -->
    <EditExtraProperties ref="EditExtraPropertiesRef" />
  </el-scrollbar>
</template>

<script lang="ts" setup>
// 引入组件区 ==========
import {
  ref,
  reactive,
  watch,
  getCurrentInstance,
  PropType,
  onMounted
} from 'vue';
import { IsEmpty,  formatDateTime } from '@/utils/common'; //导入工具函数
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';
import { funBdsJsResult } from '@/utils/Expression'; //函数公式
import EditExtraProperties from '@/components/EditExtraProperties.vue'; //配置扩展属性弹框组件
import { AppStore } from '@/stores/store';
import {  FormInstance } from 'element-plus';
import SelectDataGrid from '@/components/SelectDataGrid.vue';
import { samstoFridgeAdd, samstoFridgeEdit } from '@/views/lab_sys/samsto_set/apis/samsto_api';
// 全局变量定义区 ==========
let { proxy }: any = getCurrentInstance();
let store = AppStore();
let orgId = ref(null);
const formRef = ref<FormInstance>; //表单ref标记
const pCurSelectRow = ref({ } ); //表单ref标记
// 表单绑定的数据类型限制
interface FormDataVO {
  id: string /*主键*/;
    boxCode: String ;/*编号*/
    boxBarcode: String ;/*条码号*/
    frimodId: String ;/*冰箱型号*/
    layerNum: String ;/*架层*/
    frigTemp: String ;/*冷藏温度*/
    freezeTemp: String ;/*冷冻温度*/
    boxTypesRange: String ;/*使用标本盒类型范围*/
    seq: String ;/*位置排序*/
    status: Number ;/*状态*/
    deptName: String ;/*所属科室*/
  }
// 表单绑定的数据
let formData = reactive<FormDataVO>({
  id: '',
    boxCode:'', /*编号*/
    boxBarcode:'', /*条码号*/
    frimodId:'', /*冰箱型号*/
    layerNum:'', /*架层*/
    frigTemp:'', /*冷藏温度*/
    freezeTemp:'', /*冷冻温度*/
    boxTypesRange:'', /*使用标本盒类型范围*/
    seq:'1', /*位置排序*/
    status:1, /*状态*/
    deptName:'', /*所属科室*/
  });
let types = reactive([]);
// 表单校验规则
const formRules: any = {
  boxCode:[{ required: true, message: '请输入编号' }],
  boxBarcode:[{ required: true, message: '请输入条码号' }],
  layerNum:[{ required: true, message: '请输入架层' }],
  seq:[{ required: true, message: '请输入位置排序' }],
  deptName:[{ required: true, message: '请输入所属科室' }],
};

const EditExtraPropertiesRef = ref(null); //配置扩展属性弹框ref

defineExpose({
  addNew,
  saveForm
});

// 父组件传值
let props = defineProps({
//  infoDicts: { type: Array as PropType<any>, required: true },
  currentRow: { type: Object as PropType<any>, required: true },
  pCurSelectRow: { type: Object as PropType<any>, required: true }
}) as any;
watch(
  () => props.currentRow,
  (nval, oval) => {
    Object.assign(formData, nval);
  },
  {
    deep: true,
    immediate: true
  }
);
watch(
  () => props.pCurSelectRow,
  (nval, oval) => {
    pCurSelectRow.value=props.pCurSelectRow
  },
  {
    deep: true,
    immediate: true
  }
);

onMounted(() => {
  orgId = store.userInfo.orgId;
  //formData.bankNumber=generateCode();
});


// 方法定义区 =============
//生成题库编码
function generateCode() {
  let now = new Date();
  console.log(now,"now");
  let y = now.getFullYear(),
    m = now.getMonth() + 1,
    d = now.getDate(),
    h = now.getHours(),
    mm = now.getMinutes(),
    s = now.getSeconds(),
    ms = now.getMilliseconds();
  return "TM"+y + m+d+h+mm+s+ms;
}
function addNew(callback: any) {
  clear();
  if (callback) {
    callback({ a: '初始化完成' });
  }
}
// 清空表单
function clear() {
  //formData.bankNumber = generateCode();
  formData.id = '';//编号
  formData.boxCode = '';//编号
  formData.boxBarcode = '';//条码号
  formData.frimodId = pCurSelectRow.value.id;//冰箱型号
  formData.layerNum = '';//架层
  formData.frigTemp = '';//冷藏温度
  formData.freezeTemp = '';//冷冻温度
  formData.boxTypesRange = '';//使用标本盒类型范围
  formData.seq = '1';//位置排序
  formData.status = 1;//状态
  formData.deptName = '';//所属科室
}

// 保存信息
function saveForm() {
  submitForm();
}

// 保存
const submitForm = () => {
  let elInfo = proxy.$refs.formRef;
  formData.frimodId=pCurSelectRow.value.id;
  elInfo.validate(async (valid: any) => {
    if (!valid) {
      return false;
    }
    let res: any;
    if (IsEmpty(formData.id)) {
      res = await samstoFridgeAdd(formData);
    } else {
      res = await samstoFridgeEdit(formData);
    }
    res.status.code === 1 ? VxeMessage(res.status.msg, VxeNotifyType.success) : VxeMessage(res.status.msg, VxeNotifyType.error);
    clear();
    proxy.$emit('findList');
  });
};
function selectRow(e, rowIndex) {
  if (!IsEmpty(e)) {
    formData.deptName = e.code;
  }
}
const states = [
  {
    value: 1,
    label: '启用'
  },
  {
    value: 0,
    label: '停用'
  }
];
</script>

<style lang="scss" scoped>
// 中等尺寸时 配置扩展属性按钮
.el-button--default {
  padding: 5px 11px;
}
</style>

